import type { NextApiRequest, NextApiResponse } from 'next';
import { BeError, BeSuccess } from '@/utils/response';
import { syncProjectsLoc, syncProjectsSearch } from '@/services/gitlab';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  if (req.method?.toLocaleLowerCase() !== 'post') {
    res.status(200).json(BeError('不支持请求类型'));
    res.end();
    return;
  }
  // JSON.parse(req.body)为什么用不了
  const { body } = req;
  const keys = Object.keys(body);
  // 如果有查询条件
  if (keys.length) {
    try {
      const result = await syncProjectsSearch(body);
      res.status(200).json(BeSuccess(result));
    } catch (error: any) {
      res.status(500).json(BeError(error));
    }
  } else {
    // 没有查询条件
    const result = await syncProjectsLoc();
    res.status(200).json(BeSuccess(result));
  }
}
